package org.ielts.service.security;

import org.ielts.domain.base.Department;
import org.ielts.repository.security.DepartmentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class DepartmentService {

	@Autowired
	DepartmentRepository departmentRepository;

	@Transactional
	public Department createDepartment(String name) {
		Department department = new Department();
		department.setName(name);

		departmentRepository.save(department);

		return department;
	}

	@Transactional
	public Iterable<Department> findAll() {
		Sort sort = new Sort(new Order("name"));
		Iterable<Department> departments = departmentRepository.findAll(sort);
		return departments;
	}

	@Transactional
	public Department modifyDepartment(String id, String name) {
		Department department = departmentRepository.findOne(id);

		department.setName(name);

		return department;
	}

	@Transactional
	public void removeDepartment(String id) {
		departmentRepository.delete(id);
	}

	@Transactional
	public Department findDepartmentByName(String name) {
		Department department = departmentRepository.findOneByName(name);

		return department;
	}

}
